import {
  modalTitle,
  modalVisible,
  operateType,
  queryParams,
  tableActiveKey,
} from "./store";
import {
  drawerFormState,
  userFormState,
  dicts,
  tipsObject,
  isShowRealAuthPhoto,
  isShowSpecialPopularityLevelBtn,
  isShowAuthProcess,
  isShowAuthVideo,
  isShowAuthInfo,
  isShowIdentityInfo,
  resetFormState,
} from "@user/popular/store";
import { drawerTitle, drawerVisible } from "@user/popular/drawer";
import { tableLoading } from "@user/popular/table";
import { fetchInfo } from "./utils";
import { modalFormState } from "./store";
import { fetchDataById, fetchDicts } from "@user/popular/utils";
import { searchConfig } from "./search";
import { tableData, tableConfig, tablePage } from "./table";
import { drawerConfig } from "./drawer";
import { modalConfig } from "./modal";
const showDrawer = () => (drawerVisible.value = true);
const showModal = () => (modalVisible.value = true);
export const refreshTable = async (searchParams: any = {}) => {
  tablePage.current = 1;
  initObjAttr(queryParams, searchParams);
  await fetchList();
};
export const fetchList = async () => {
  try {
    tableData.value.length = 0;
    tableLoading.value = true;
    queryParams.page = tablePage.current;
    queryParams.page_size = tablePage.pageSize;
    const res: any = await authApi.query(queryParams);
    if (res.code != 200) return message.error(res.message);
    tableData.value = res?.data?.list ?? [];
    tablePage.total = res?.data?.attributes?.total ?? 0;
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};

export const handleAction = async ({ record, type }: any) => {
  try {
    operateType.value = type;
    switch (tableActiveKey.value) {
      case 0:
        switch (trimAll(type)) {
          case "view":
            resetFormState(drawerFormState);
            drawerTitle.value = $t("base.realPersonAuthDetail");
            resetObjAttr(userFormState);
            await fetchDataById({
              real_person_id: record.real_person_id,
            });
            const res = await fetchDicts();
            dicts.value = res;
            if (!isEmptyType(record)) {
              modalFormState.real_person_id = record.real_person_id;
            }
            isShowRealAuthPhoto.value = true;
            isShowSpecialPopularityLevelBtn.value = false;
            // isNotShowAuthTime.value = false;
            isShowAuthProcess.value = false;
            isShowAuthVideo.value = false;
            isShowAuthInfo.value = true;
            isShowIdentityInfo.value = false;
            showDrawer();
            break;
          case "agree":
            modalTitle.value = $t("base.agreeRealPersonAuth");
            if (!isEmptyType(record)) {
              modalFormState.real_person_id = record.real_person_id;
            }
            showModal();
            break;
          case "refuse":
            modalTitle.value = $t("base.refuseRealPersonAuth");
            modalFormState.reason = undefined;
            if (!isEmptyType(record)) {
              modalFormState.real_person_id = record.real_person_id;
            }
            showModal();
            break;
          case "markLevel":
            modalTitle.value = $t("base.specifyPopularityLevel");
            modalFormState.editId = record.id;
            tableLoading.value = true;
            await fetchInfo();
            tableLoading.value = false;
            showModal();
            break;
        }
        break;
      case 1:
        break;
    }
  } catch (error: any) {
    message.error(error.message);
  }
};
export default defineComponent({
  setup() {
    const defineSearch = searchConfig();
    const defineTable = tableConfig();
    const defineDrawer = drawerConfig();
    const defineModal = modalConfig();
    watch(
      () => sysZone.value,
      () => fetchList()
    );
    onActivated(() => {
      tipsObject.title = $t("auth1");
      tipsObject.content = $t("auth2");
      fetchList();
    });
    return () => (
      <>
        <s-design
          defineSearch={defineSearch}
          defineTable={defineTable}
          defineDrawer={defineDrawer}
          defineModal={defineModal}
        />
      </>
    );
  },
});
